﻿/*
 * Author: Martijn Koopman
 * 
 * Description:
 * */

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace LibraryCS {
    public class ImpulsiveNoise : NoiseAlgorithm {
        public int percentagePixels;

        public ImpulsiveNoise(string name) : base(name) { }

        public override Bitmap DoAlgorithm(Bitmap sourceImage) {
            //VImage sourceImageOps = new VImage(sourceImage);

            Bitmap resultImage = new Bitmap(sourceImage);
            VImage resultImageOps = new VImage(resultImage);

            /* ********** \/ DO ALGORITHM \/ ********** */
            //double percentage = 20;
            int numPixels = sourceImage.Width * sourceImage.Height;
            percentagePixels = (int)(numPixels * (NoisePercentage / 100));
            Random random = new Random();

            //Console.Out.WriteLine("numPixels: " + numPixels);
            //Console.Out.WriteLine("percentagePixels: " + percentagePixels);

            for (int i = 0; i < percentagePixels; ) {
                // Get random pixel
                int randomValue = random.Next(numPixels);
                //if (noisePixels[randomValue] > 0) continue;
                // Make pixel white
                //resultImageOps.imageARGB[randomValue] = 0xFF << 24 | 0x000000;
                resultImageOps.RedPixels[randomValue] = 0;
                resultImageOps.GreenPixels[randomValue] = 0;
                resultImageOps.BluePixels[randomValue] = 0;
                //noisePixels[randomValue] = 1;

                i++;
                // Get random pixel
                randomValue = random.Next(numPixels);
                //if (noisePixels[randomValue] > 0) continue;
                // Make pixel black
                //resultImageOps.imageARGB[randomValue] = 0xFF << 24 | 0xFFFFFF;
                resultImageOps.RedPixels[randomValue] = 255;
                resultImageOps.GreenPixels[randomValue] = 255;
                resultImageOps.BluePixels[randomValue] = 255;
                //noisePixels[randomValue] = 2;
                i++;
            }

            //noisePercent = (double)percentagePixels / (double)noisePixels.Length;
            //noisePercent *= 100;

            resultImageOps.ApplyPixelChanges();
            return resultImageOps.localBitmap;
        }

        #region GUI Functions
        public override SettingsPropertyBox createControls() {
            SettingsPropertyBox Controls = new SettingsPropertyBox();
            // Create a Label indictating the type of setting this is
            Label lb = new Label();
            lb.Text = "Noise Percentage";
            lb.Name = "lbNoisePercentage";

            // Create a Label that contains the value of the trackbar
            Label tlb = new Label();
            tlb.Dock = DockStyle.Right;
            tlb.Text = "50%";
            lb.Name = "lbTBNoisePercentage";

            // Create a TrackBar Control for setting the percentage of noise
            TrackBar tbr = new TrackBar();
            tbr.Name = "tbrNoisePercentage";
            tbr.SetRange(0, 100);
            tbr.TickFrequency = 10;
            tbr.SmallChange = 5;
            tbr.LargeChange = 10;
            tbr.Value = 50;
            tbr.Size = new Size(250, 25);

            // Add the controls to the list
            //      Controls.Add(tbr);
            //      Controls.Add(lb);
            //     Controls.Add(tlb);

            Controls.AddLine(lb, tlb, tbr, null);
            // Return the array
            return Controls;
        }

        public override void setSettings(List<object> settings) {
            try {
                NoisePercentage = Int32.Parse((string)settings[0]);

                Debug.WriteLine("Settings entered: " + NoisePercentage);
            } catch (FormatException) {
                MessageBox.Show("Wrong Format has been entered.\nPlease enter integers\nNew Settings have been discarded for Median", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }
        #endregion
    }
}
